﻿module Sustainalytics.Routing {

    export class RouteSecurity {

        /** Resources required to be available for a route to be accessible */
        public resources: RouteSecurityResource[];

        /** User defined function used to check if the provided path is valid */
        public securityFunc: (security: RouteSecurity, $injector: any) => ng.IPromise<boolean>;

        /** Action to be executed when the route is not allowed. Will override the default action which is to redirect to login page. */
        public notAllowedAction: (path: string, routeSecurity: RouteSecurity) => ng.IPromise<any>;

        /** Logout when the route is reached. */
        public logout: boolean = false;
        public noRedirect: boolean = false;

        /** security services used to validate current route */
        public securityServices: string[] = [];


        constructor() {
            this.resources = new Array();
        }

        public addResource(res: RouteSecurityResource): RouteSecurity {
            this.resources.push(res);
            return this;
        }

        public addAdminResource(): RouteSecurity {
            this.resources.push({ verb: 'GET', resource: 'api/v1/admin', parameters: [] });
            return this;
        }

        public setFunction(securityFunc: (security: RouteSecurity, $injector: any) => ng.IPromise<boolean>): RouteSecurity {
            this.securityFunc = securityFunc;
            return this;
        }

        public setNotAllowedAction(notAllowedAction: (path: string, routeSecurity: RouteSecurity) => ng.IPromise<any>) {
            this.notAllowedAction = notAllowedAction;
            return this;
        }

        public setLogout(logout: boolean, noRedirect?:boolean): RouteSecurity {
            this.logout = logout;
            this.noRedirect = noRedirect;
            return this;
        }

        public addService(serviceName: string) {
            this.securityServices.push(serviceName);
            return this;
        }
    }
}